<?php 
namespace app\admin\controller;

use app\admin\model\MenuModel;
use app\admin\logic\MenuLogic;

/**
 * 后台菜单管理
 */
class Menu extends Base
{
	
	public function index()
	{
		$menu = new MenuModel();
		$logic  = new MenuLogic();

		$type     = input("param.type/d");
		$pagenum  = input("param.page/d") ;
		$pagesize = input("param.limit/d");
		$pid      = input("param.pid/d");

		if ($type == 1) {
			$pagenum  = $pagenum < 1 ? 1: $pagenum;
			$pagesize = $pagesize < 1 ? 10: $pagesize;
			$start    = ($pagenum - 1) * $pagesize;
			$size     = $pagesize;
			$where    = !empty($pid)? "pid = ".$pid: '1=1';

			$sum    = $menu->count_menu_list_tree($where);
			$res    = $menu->get_menu_list_tree($where, $start, $size);
			$parent = $logic->get_parent_arr();

			foreach ($res as $k => $v) {
				$res[$k]['parent'] = $parent[$v['pid']];
			}

            $data = array();
            $data['code']   = '0';
            $data['msg']    = '';
            $data['count']  = $sum;
            $data['data']   = $res;

            echo json_encode($data);
            exit;
		}
		// 父级菜单
		$parent = $logic->get_parent_list();

		return $this->fetch('index', ['parent' => $parent]);
	}
	

	// 添加菜单
	public function edit()
	{
		// 提交数据处理
		$flag   = input("param.flag/s");
		$nav_id = input("param.nav_id/d");
		$menu   = new MenuModel();
		$logic  = new MenuLogic();

		if (request()->isAjax()) {

			// 获取数据
			$data = input("param.");
			// 验证信息
			$res = $this->validate($data, \app\admin\validate\Menu::class);
			if ($res !== true) {
				// 返回错误信息
				return json_encode(msg_error($res));
			}
			// 菜单等级
			$data['level'] = $data['pid'] > 1? 2: 1;

			if ($flag == "add") {

				$res  = $menu->insert_data($data);// 添加数据
				if (is_array($res)) return json_encode($res);
					
			}elseif($flag == "edit"){
				$res  = $menu->save_data($data);// 更新数据
				if (isset($res['status'])) return json_encode($res);
			}

			echo json_encode(msg_success("编辑成功"));
			exit;
		}

		// 获取一级菜单
		$parents = $logic->get_parent_list();

		$data = [];
		$data['parents'] = $parents;
		if ($flag == "add") {

			return $this->fetch('add', $data);

		}elseif($flag == "edit"){
			// 获取该条数据详情
			$result = $menu::get($nav_id);
			$data['data'] = $result;

			return $this->fetch('edit', $data);
		}
	}

	// 删除&批量删除
	public function delmenu()
	{
		$menu = new MenuModel();
		$id   = input("param.nav_id/d");
		$ids  = input("param.ids");

		if (!empty($id)) {
			$where = ['nav_id' => $id];
		}

		if (!empty($ids)) {
			$ids   = json_decode($ids);
			$where = "nav_id IN (".implode(",", $ids).")";
		}

		// 删除数据
		$res = $menu->del_data($where);
		if (isset($res['msg'])) {
			echo json_encode($res);
		}else{
			echo json_encode(msg_success("删除成功"));
		}
		exit;
	}

	// 获取父级菜单
	public function ajax_get_parent_menu()
	{
		if (request()->isAjax()) {
			
			$level = input("param.level");
			$level = empty($level)? 0: $level;
			$wherearr = array("level" => $level);
			$fields = "nav_id,name";

			$menu = new MenuModel();
			$res  = $menu->get_list($wherearr,$fields);

			echo json_encode($res);
			exit;
		}
	}
	
}


 ?>